﻿using System;
using RootMotion.FinalIK;
using UnityEngine;

namespace RootMotion.Demos
{
	// Token: 0x02000099 RID: 153
	[RequireComponent(typeof(FullBodyBipedIK))]
	[RequireComponent(typeof(AimIK))]
	public class AnimatorController3rdPersonIK : AnimatorController3rdPerson
	{
		// Token: 0x06000541 RID: 1345 RVA: 0x00005503 File Offset: 0x00003703
		public void OnGUI()
		{
			GUILayout.Label("Press F to switch Final IK on/off", new GUILayoutOption[0]);
		}

		// Token: 0x06000542 RID: 1346 RVA: 0x00034184 File Offset: 0x00032384
		public override void Start()
		{
			base.Start();
			this.aim = base.GetComponent<AimIK>();
			this.ik = base.GetComponent<FullBodyBipedIK>();
			this.aim.enabled = false;
			this.ik.enabled = false;
			this.headLookAxis = this.ik.references.head.InverseTransformVector(this.ik.references.root.forward);
			this.animator.SetLayerWeight(1, 1f);
		}

		// Token: 0x06000543 RID: 1347 RVA: 0x00034208 File Offset: 0x00032408
		public override void Move(Vector3 moveInput, bool isMoving, Vector3 faceDirection, Vector3 aimTarget)
		{
			base.Move(moveInput, isMoving, faceDirection, aimTarget);
			this.aimTarget = aimTarget;
			if (Input.GetKeyDown(KeyCode.F))
			{
				this.useIK = !this.useIK;
			}
			if (!this.useIK)
			{
				return;
			}
			this.Read();
			this.AimIK();
			this.FBBIK();
			this.HeadLookAt(aimTarget);
		}

		// Token: 0x06000544 RID: 1348 RVA: 0x0003426C File Offset: 0x0003246C
		public void Read()
		{
			this.leftHandPosRelToRightHand = this.ik.references.rightHand.InverseTransformPoint(this.ik.references.leftHand.position);
			this.leftHandRotRelToRightHand = Quaternion.Inverse(this.ik.references.rightHand.rotation) * this.ik.references.leftHand.rotation;
		}

		// Token: 0x06000545 RID: 1349 RVA: 0x00005515 File Offset: 0x00003715
		public void AimIK()
		{
			this.aim.solver.IKPosition = this.aimTarget;
			this.aim.solver.Update();
		}

		// Token: 0x06000546 RID: 1350 RVA: 0x000342E4 File Offset: 0x000324E4
		public void FBBIK()
		{
			Quaternion rotation = this.ik.references.rightHand.rotation;
			Vector3 a = this.ik.references.rightHand.TransformPoint(this.leftHandPosRelToRightHand);
			this.ik.solver.leftHandEffector.positionOffset += a - this.ik.references.leftHand.position;
			Vector3 vector = this.ik.references.rightHand.rotation * this.gunHoldOffset;
			this.ik.solver.rightHandEffector.positionOffset += vector;
			this.ik.solver.leftHandEffector.positionOffset += vector + this.ik.references.rightHand.rotation * this.leftHandOffset;
			this.ik.solver.Update();
			this.ik.references.rightHand.rotation = rotation;
			this.ik.references.leftHand.rotation = rotation * this.leftHandRotRelToRightHand;
		}

		// Token: 0x06000547 RID: 1351 RVA: 0x00034430 File Offset: 0x00032630
		public void HeadLookAt(Vector3 lookAtTarget)
		{
			Quaternion b = Quaternion.FromToRotation(this.ik.references.head.rotation * this.headLookAxis, lookAtTarget - this.ik.references.head.position);
			this.ik.references.head.rotation = Quaternion.Lerp(Quaternion.identity, b, this.headLookWeight) * this.ik.references.head.rotation;
		}

		// Token: 0x04000413 RID: 1043
		[SerializeField]
		public bool useIK = true;

		// Token: 0x04000414 RID: 1044
		[Range(0f, 1f)]
		public float headLookWeight = 1f;

		// Token: 0x04000415 RID: 1045
		public Vector3 gunHoldOffset;

		// Token: 0x04000416 RID: 1046
		public Vector3 leftHandOffset;

		// Token: 0x04000417 RID: 1047
		public AimIK aim;

		// Token: 0x04000418 RID: 1048
		public FullBodyBipedIK ik;

		// Token: 0x04000419 RID: 1049
		public Vector3 headLookAxis;

		// Token: 0x0400041A RID: 1050
		public Vector3 leftHandPosRelToRightHand;

		// Token: 0x0400041B RID: 1051
		public Quaternion leftHandRotRelToRightHand;

		// Token: 0x0400041C RID: 1052
		public Vector3 aimTarget;
	}
}
